package com.movitech.mobile.cz.base.sys.controller;


import com.movitech.mobile.cz.base.common.util.PageCode;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.sys.bizvo.RegionalCompanyRequestVO;
import com.movitech.mobile.cz.base.sys.bizvo.RegionalCompanySaveRequestVO;
import com.movitech.mobile.cz.base.sys.entity.SProject;
import com.movitech.mobile.cz.base.sys.entity.SRegionalCompany;
import com.movitech.mobile.cz.base.sys.service.SRegionalCompanyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/**
 * <p>
 * SRegionalCompany 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@RestController
@RequestMapping("/v1/sRegionalCompany")
@Api(value = "地区公司基础操作接口", description = "地区公司基础操作接口")
public class SRegionalCompanyController {
    @Autowired
    @Qualifier("sRegionalCompanyService")
    private SRegionalCompanyService sRegionalCompanyService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有地区公司接口",
            notes = "获取所有地区公司接口",
            response = SRegionalCompany.class
    )
    public RestReturnResult list() {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SRegionalCompany> page = sRegionalCompanyService.selectPage(new Page<SRegionalCompany>());
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @PostMapping("/searchList/{pageNumber}")
    @ApiOperation(
            value = "地区公司列表查询接口",
            notes = "地区公司列表查询接口",
            response = SRegionalCompany.class
    )
    public RestReturnResult searchList(@RequestBody SRegionalCompany sRegionalCompany,
                @ApiParam(value = "分页页数")@PathVariable Integer pageNumber) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SRegionalCompany> page = new Page<>();
        page.setCurrent(pageNumber);
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(sRegionalCompany);
        page = sRegionalCompanyService.selectPage(page,wrapper);
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @GetMapping("/getSRegionalCompany/{id}")
    @ApiOperation(
            value = "地区公司对象ID获取接口",
            notes = "地区公司对对象ID获取接口",
            response = SRegionalCompany.class
    )
    public RestReturnResult getSRegionalCompany(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        SRegionalCompany sRegionalCompany = sRegionalCompanyService.selectById(id);
        restReturnResult.setData(sRegionalCompany);
        return restReturnResult;
    }

    @PostMapping("/add")
    @ApiOperation(
            value = "地区公司添加接口",
            notes = "地区公司添加接口",
            response = SRegionalCompany.class
    )
    public RestReturnResult add(@RequestBody SRegionalCompany sRegionalCompany) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sRegionalCompanyService.insert(sRegionalCompany);
        restReturnResult.setData(sRegionalCompany);
        return restReturnResult;
    }

    @PutMapping("/update")
    @ApiOperation(
            value = "地区公司更新接口",
            notes = "地区公司更新接口",
            response = SRegionalCompany.class
    )
    public RestReturnResult update(@RequestBody SRegionalCompany sRegionalCompany) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sRegionalCompanyService.updateById(sRegionalCompany);
        restReturnResult.setData(sRegionalCompany);
        return restReturnResult;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(
            value = "地区公司通过ID删除接口",
            notes = "地区公司通过ID删除接口",
            response = SRegionalCompany.class
    )
    public RestReturnResult update(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sRegionalCompanyService.deleteById(id);
        return restReturnResult;
    }

    /**
	 * 地区公司列表接口
	 * @return
	 */
    @ApiOperation(
            value = "地区公司列表接口",
            notes = "地区公司列表接口",
            response = SProject.class
    )
	@RequestMapping(value = "/public/getRegionalCompanyList", method = RequestMethod.POST)
	@ResponseBody
	public RestReturnResult<Object> getRegionalCompanyList(@RequestBody RegionalCompanyRequestVO regionalCompanyRequestVO){
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

		try{
	        PageCode pageCode = sRegionalCompanyService.findRegionalCompanyList(regionalCompanyRequestVO);

	        hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, pageCode);
		}catch(Exception e){
			e.printStackTrace();
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
		}

		return hResult;
	}

    @ApiOperation(value = "地区公司更新接口", notes = "地区公司更新接口")
    @RequestMapping(value = "/saveOrUpdateRegionalCompany", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult<Object> saveOrUpdateRegionalCompany(@RequestBody RegionalCompanySaveRequestVO regionalCompanySaveRequestVO){
    	RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
    	
    	try{
    		hResult = sRegionalCompanyService.saveOrUpdateRegionalCompany(regionalCompanySaveRequestVO);
    	}catch(Exception e){
    		e.printStackTrace();
    		hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
    	}
    	
    	return hResult;
    }

}

